﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Configuration;

namespace SEO.EmailSearch
{
    public partial class KeywordImportFrm : Form
    {
        delegate void SetTextCallback(string text);
        private Thread catchThread = null;
        SEO.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");

        public KeywordImportFrm()
        {
            InitializeComponent();
            this.Icon = new Icon(SEO.Utility.ICONHelper.SetFormICON());
            skinEngine1.SkinFile = SEO.Utility.CommonHelper.GetCurrentSoftSkin();
        }

        /// <summary>
        /// 显示实时消息到showMsg
        /// </summary>
        /// <param name="strMessage">实时消息</param>
        private void SetText(string strMessage)
        {
            if (this.showMsg.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { strMessage });
            }
            else
            {
                this.showMsg.Text = strMessage;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件(*.txt)|*.txt";
            openFileDialog.ShowDialog();
            string strUploadFile = openFileDialog.FileName;
            txtFile.Text = strUploadFile;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            catchThread = new Thread(new ThreadStart(ImportData));
            catchThread.Start();
        }

        /// <summary>
        /// 导入关键词
        /// </summary>
        private void ImportData()
        {
            try
            {
                string strGetUserID = iniFile.GetString("INFO", "userid", "");
                int iNum = 1;
                string path = txtFile.Text; //定义上传路径  
                StreamReader smRead = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312")); //设置路径  
                string line;
                while ((line = smRead.ReadLine()) != null)
                {
                    string strLineData = line.Trim();
                    string strKeyword = strLineData;

                    SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + "【" + iNum + "】正在导入 -> " + strKeyword);
                    this.SetText(SEO.Utility.PublicValue.GetMessage());

                    string strStatus = "";
                    try
                    {
                        //加入数据库
                        SEO.DAL.ESKeywordsDAL keyworddal = new DAL.ESKeywordsDAL();
                        if (!keyworddal.CheckEmailAddressIsExist(strKeyword))
                        {
                            SEO.BLL.ESKeywordsBLL keywordbll = new BLL.ESKeywordsBLL();
                            SEO.Model.ESKeywords keyword = new Model.ESKeywords();
                            keyword.KeyID = keyworddal.GetAutoAddNum();
                            keyword.KeyName = strKeyword;
                            keywordbll.InsertESKeywordsBLL(keyword);
                            strStatus = "成功！";
                        }
                        else
                        {
                            strStatus = "重复！";
                        }
                    }
                    catch (Exception ex)
                    {
                        strStatus = "失败！";
                        SEO.Utility.Log.SaveErrorLog("添加关键词 -> " + ex.Message);
                    }

                    iNum++;

                    SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 状态 -> " + strStatus);
                    this.SetText(SEO.Utility.PublicValue.GetMessage());
                }

                SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 读取完毕！ ");
                this.SetText(SEO.Utility.PublicValue.GetMessage());
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("导入关键词 -> " + ex.Message);
            }
        }

        private void KeywordImportFrm_Load(object sender, EventArgs e)
        {

        }

    }
}
